import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../config/base_configuration.dart';

class ImageWithPlaceHolder extends StatelessWidget {
  final String cover;
  final double? height;
  final double? width;
  final BoxFit? fit;
  final double radius;

  ImageWithPlaceHolder({
    Key? key,
    this.cover: "",
    this.width,
    this.height,
    this.fit: BoxFit.cover,
    this.radius: 12,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return CachedNetworkImage(
      imageUrl: cover,
      fit: fit ?? null,
      height: height ?? null,
      width: width ?? null,
      placeholder: (
        BuildContext context,
        String url,
      ) {
        return placeholder();
      },
      errorWidget: (
        BuildContext context,
        String url,
        dynamic error,
      ) {
        //没有地址返回一个公司icon
        return placeholder();
      },
    );
  }

  Widget placeholder() {
    return ImagePlaceholder(
      height: height ?? null,
      width: width ?? null,
      radius: radius,
    );
  }
}

class ImagePlaceholder extends StatelessWidget {
  final double? height;
  final double? width;
  final double radius;
  final Color? color;

  ImagePlaceholder(
      {Key? key, this.height, this.width, this.radius: 12, this.color})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      height: height ?? null,
      width: width ?? null,
      decoration: BoxDecoration(
        color: color ?? ColorConf.shallowGray,
        borderRadius: BorderRadius.circular(radius.w),
        image: DecorationImage(
          scale: 2,
          image: const AssetImage('images/lt_common/temp_load.png'),
        ),
      ),
    );
  }
}
